function [max_v, history] = calculate_max_vstay(schedule, G, code_map)
    v_stay = 0; max_v = 0;
    if isempty(schedule), return; end
    history = zeros(1, length(schedule));
    for i = 1:length(schedule)
        node_id = schedule(i);
        op = G.Nodes.OpCode(node_id); type = G.Nodes.TypeCode(node_id);
        if isequal(type, code_map.type.L1) || isequal(type, code_map.type.UB)
            if isequal(op, code_map.op.ALLOC), v_stay = v_stay + G.Nodes.Size(node_id);
            elseif isequal(op, code_map.op.FREE), v_stay = v_stay - G.Nodes.Size(node_id); end
        end
        if v_stay > max_v, max_v = v_stay; end
        history(i) = v_stay;
    end
end